<?php

/**
 * $Id: content.php 4015 2012-02-20 02:39:04Z minhnguyen $
 */
require_once('frontend.php');

class Balatra extends Frontend {

    function Balatra() {
        parent::Frontend();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->model('Mcategory');
        $this->load->model('Mproducts');
        $this->load->model('Marticle');
        $this->load->model('Marticle_attribute');
        $this->load->model('Mimage');
        $this->load->model('Moption_email');
        $this->data['menu_active'] = "";
    }

    public function createSubMenu() {
        $strUl = '<ul>';

        $strUl .= '</ul>';
    }

    public function index($link = NULL, $detail = NULL, $page = 1) {
        $menu = $this->Mcategory->getAllCategory();
        $this->recursive($menu, 0, 1, $newstr);
        if (!empty($newstr)) {
            foreach ($newstr as $key => $info) {
                if ($info['friendly_url'] == 'trang-chu')
                    unset($newstr[$key]);
            }
        }

        $this->Menu1($newstr, 0, $newstr1);
        $this->data['str_menu'] = str_replace('<ul></ul>', '', $newstr1);

//        $this->getNavigation();
        if ($link == NULL || $link == 'trang-chu') {
            $this->data['ishome'] = 'home';
            $this->layout[AppContants::LAYOUT_MAINVIEW] = 'home';
        } else {
            switch ($link) {
                case 'gioi-thieu':
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = 'introdue-detail';
                    $aticle = $this->Marticle->getArticleDetailByCategoryUrl($link);
                    $this->data['article'] = $aticle;
                    break;
                case 'tin-tuc-su-kien':
                    $aticles = $this->Marticle->getAllArticleByCategoryUrl($link);
                    $this->data['total_page'] = ceil((int) count($aticles) / 3);
                    $this->data['articles'] = $aticles;

                    if ($detail == NULL || $detail == 'page') {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Ba lá trà/Tin tức sự kiện", "trang-chu/tin-tuc-su-kien");
                        $this->load->library('pagination');
                        $this->load->library('paginationlib');

                        $pagingConfig = $this->paginationlib->initPagination("tin-tuc-su-kien/page", count($aticles));

                        $articles = $this->Marticle->list_all($link, ($page - 1) * $pagingConfig['per_page'], $pagingConfig['per_page']);
                        $this->data['articles'] = $articles;


                        $this->layout[AppContants::LAYOUT_MAINVIEW] = 'news-list';
                    } else {
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = 'news-detail';
                        $article = $this->Marticle->getArticleDetailByFriendlyUrl($detail);
                        $this->data['article'] = $article;
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Ba lá trà/Tin tức sự kiện/" . $article['name'], "trang-chu/tin-tuc-su-kien/" . $article['friendly_url']);
                    }
                    break;
                case 'lien-he':
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = 'contact';
                    if ($detail == 'submit') {
                        echo 'Xu ly send mail';
                        die;
                    }
                    break;
                default :
                    break;
            }
        }
    }

    public function processProduct($category = NULL, $detail = NULL) {
        $this->getNavigation();
        if ($category == NULL) {
            $category_arr = $this->Mcategory->getFirstSubcategoryOfCategory(3);
            $category = $category_arr['friendly_url'];
        }

        $products = $this->Mproducts->getAllProductsByCategoryURL($category);
        $this->data['products'] = $products;

        if ($detail == NULL) {
            if ($category == NULL)
                $this->data['breadcrumb'] = $this->createBreadcrunb("Ba lá trà/Thuc Don", "trang-chu/thuc-don");
            else
                $this->data['breadcrumb'] = $this->createBreadcrunb("Ba lá trà/Thuc don/" . $category, "trang-chu/thuc-don/" . $category);

            $this->layout[AppContants::LAYOUT_MAINVIEW] = 'product-list';
        } else {
            $this->layout[AppContants::LAYOUT_MAINVIEW] = 'product-detail';
            $this->data['breadcrumb'] = $this->createBreadcrunb("Ba lá trà/Thuc don/" . $category . '/' . $detail, "trang-chu/thuc-don/" . $category . '/' . $detail);
            //get product detail
            $product = $this->Mproducts->getProductDetailByProductUrl($detail);
            $this->data['product'] = $product;
            //get all image of product
            $product_imgage = $this->Mimage->getAllImagesByProductUrl($detail);
            $this->data['product_imgage'] = $product_imgage;
            //$this->print_debug($product_imgage);
        }
    }

    public function getNavigation() {
        $level = $this->Mcategory->getLevel();
        $category['level1'] = $this->Mcategory->getCategoryByLevel($level[0]['level']);
        for ($i = 1; $i < count($level); $i++) {
            for ($j = 0; $j < count($category['level' . $i]); $j++) {
                $t = $this->Mcategory->getCategoryByLevel($level[$i]['level'], $category['level' . $i][$j]['id']);
                if (!empty($t))
                    $category['level' . $i][$j]['level' . ($i + 1)] = $t;
            }
        }
        $this->data['category'] = $category;
        $this->data['level'] = $level;
    }

}

?>